﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using AbpVueDemo.JichuxinxiDome.XianluDataManager.Dto;
using AbpVueDemo.JichuxinxinDemo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbpVueDemo.JichuxinxiDome.XianluDataManager
{
    public class XianluDataService : AsyncCrudAppService<XianluData, XianluDataInputDto, int, PageXianluDataRequestDto, XianluDataCreateDto, XianluDataUpdateDto>, IXianluDataService
    {
        private IRepository<XianluData, int> _repositoryXianluData;
        private IRepository<MingNameData, int> _repositoryMingNameData;
        public XianluDataService(
        IRepository<XianluData, int> repository,IRepository<XianluData,int> repositoryXianluData,IRepository<MingNameData> repositoryMingNameData) : base(repository)
        {
            _repositoryXianluData = repositoryXianluData;
            _repositoryMingNameData = repositoryMingNameData;
        }
        /// <summary>
        /// 自定义分页条件查询, 需要重写父类的CreateFilteredQuery方法,让其进行自定义条件筛选
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns></returns>
        //protected override IQueryable<XianluData> CreateFilteredQuery(PageXianluDataRequestDto input)
        //{
        //    return base.CreateFilteredQuery(input)

        //         .WhereIf(!string.IsNullOrEmpty(input.x_xianluname), c => c.x_xianluname.Contains(input.x_xianluname))
        //          .WhereIf(!string.IsNullOrEmpty(input.x_qidian), c => c.x_qidian.Contains(input.x_qidian))
        //           .WhereIf(!string.IsNullOrEmpty(input.x_zhongdian), c => c.x_zhongdian.Contains(input.x_zhongdian))
        //            .WhereIf(input.x_sfwaixie > 0, c => c.x_sfwaixie == input.x_sfwaixie)
        //             .WhereIf(!string.IsNullOrEmpty(input.x_huophone), c => c.x_huophone.Contains(input.x_huophone))
        //              .WhereIf(!string.IsNullOrEmpty(input.x_huodanwei), c => c.x_huodanwei.Contains(input.x_huodanwei));
        //}

        public async Task<PagedResultDto<LianchaDto>> GetMyPageAsync(PageXianluDataRequestDto input)
        {
            var xianlu = _repositoryXianluData.GetAll();
            var mingname = _repositoryMingNameData.GetAll();
            var query = (from a in xianlu
                         join b in mingname on a.x_huoid equals b.Id
                         select new { a, b }
                       ).AsQueryable();//查询ab两表全部数据
            query = query.WhereIf(!string.IsNullOrEmpty(input.x_xianluname), c => c.a.x_xianluname.Contains(input.x_xianluname));
            query = query.WhereIf(!string.IsNullOrEmpty(input.x_qidian), c => c.a.x_qidian.Contains(input.x_qidian));
            query = query.WhereIf(!string.IsNullOrEmpty(input.x_zhongdian), c => c.a.x_zhongdian.Contains(input.x_zhongdian));
            query = query.WhereIf(input.x_sfwaixie>0, c => c.a.x_sfwaixie==input.x_sfwaixie);
            query = query.WhereIf(!string.IsNullOrEmpty(input.x_huophone), c => c.a.x_huophone.Contains(input.x_huophone));
            query = query.WhereIf(!string.IsNullOrEmpty(input.x_huodanwei), c => c.a.x_huodanwei.Contains(input.x_huodanwei));
            int totalCount = query.Count();
            query = query.PageBy(input);
            List<LianchaDto> liancha = query.Select(c => new LianchaDto() { XianluData = c.a, MingNameData = c.b }).ToList();
            var result = new PagedResultDto<LianchaDto>()
            {
                Items= liancha,
                TotalCount= totalCount
            };
            return result;
        }
    }
}
